package com.example.demo.practice.area;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ExpandableListView;

import com.example.demo.practice.R;
import com.example.demo.practice.innerclass.IBaseActivity;
import com.example.demo.practice.innerclass.NormalExpandableListAdapter;
import com.example.demo.practice.tool.ServerHttp;
import com.example.demo.practice.ui.MapAndExpList;
import com.example.demo.practice.ui.MapAndList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class AdDivision extends IBaseActivity {
    private MapAndExpList mapAndList;
    private List<List<String>> coordinates;
    private List<String> groupData;
    private List<List<String>> childData;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            JSONArray jsonArray = serverHttp.getData();
            groupData = new ArrayList<String>();
            childData = new LinkedList<List<String>>();
            coordinates = new LinkedList<List<String>>();
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject json = null;
                try {
                    json = (JSONObject) jsonArray.get(i);
                    String adName = json.getString("adname");
                    String name = json.getString("name");
                    if (groupData.contains(adName)) {
                        for (int j = 0; j < groupData.size(); j++) {
                            if (groupData.get(j).equals(adName) && (!childData.get(j).contains(name))) {
                                childData.get(j).add(name);
                                coordinates.get(j).add(json.getString("geom"));
                                break;
                            }
                        }
                    } else {
                        groupData.add(adName);
                        List<String> list = new ArrayList<String>();
                        List<String> coordinate = new ArrayList<String>();
                        list.add(name);
                        coordinate.add(json.getString("geom"));
                        childData.add(list);
                        coordinates.add(coordinate);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            NormalExpandableListAdapter normalExpandableListAdapter = new NormalExpandableListAdapter(groupData, childData);
            mapAndList.setListAdapter(normalExpandableListAdapter);
            mapAndList.setChildrenClick(new ChildrenClick());
            serverHttp.clear();
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ad_division);
        setTitle("行政划分");
        mapAndList = (MapAndExpList) findViewById(R.id.mapAndList);
        mapAndList.loadUrl("html/area/AdDivision.html");
        serverHttp = new ServerHttp(handler);
        serverHttp.setUrl("district");
    }

    public class ChildrenClick implements ExpandableListView.OnChildClickListener{

        @Override
        public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
            String geom = String.valueOf(coordinates.get(i).get(i1));
            int s1 = geom.indexOf("[");
            int s2 = geom.indexOf("]");
            String coordinates = geom.substring(s1 + 1, s2);
            String[] latLng = coordinates.split(",");
            double lng = Double.parseDouble(latLng[0]);
            double lat = Double.parseDouble(latLng[1]);
            mapAndList.setZoomCenter(lat, lng, 11);
            return false;
        }
    }
}
